package gof_003_行为型_Behavioral.gof_003_观察者_Observer.gof_002_Observer;

import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) {
        // 开始观察
        Lamp lamp = observe();

        //修改状态
        lamp.setColor("绿");
        sleep(2);

        lamp.setColor("黄");
        sleep(2);

        lamp.setColor("红");
    }


    /**
     * 构建观察
     * 将所有观察者加入列表
     */
    public static Lamp observe() {
        Lamp lamp = new Lamp();

        LampObserver xiaoHong = new Observer_XiaoHong();
        LampObserver xiaoMing = new Observer_XiaoMing();
        LampObserver xiaoGang = new Observer_XiaoGang();

        lamp.addObserver(xiaoHong);
        lamp.addObserver(xiaoMing);
        lamp.addObserver(xiaoGang);
        return lamp;
    }

    /**
     * 睡眠几秒
     *
     * @param seconds
     */
    public static void sleep(int seconds) {
        try {
            TimeUnit.SECONDS.sleep(seconds);
            System.out.println();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
